package backend.service.other;

import backend.model.other.MonthState;
import backend.service.base.DBModelServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class MonthStateServiceImpl extends DBModelServiceImpl<MonthState> implements MonthStateService {

    @Override
    public Class<MonthState> getEntityClass() {
        return MonthState.class;
    }

    @Override
    public MonthState findMonthStateByDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        int year = calendar.get(Calendar.YEAR);
        List<MonthState> monthStateList = dbModelDao.findByNamedQueryAndParams("findMonthStateByMonthAndYear", new String[]{"month", "year"}, new Object[]{month, year});
        if (monthStateList.size() == 1) {
            return monthStateList.get(0);
        } else if (monthStateList.size() == 0){
            return null;
        }
        throw new RuntimeException("More than one MonthState found for month " + month + "/" + year);
    }

    @Override
    public MonthState findMonthStateByMonthAndYear(int monthOneBased, int year) {
        List<MonthState> monthStateList = dbModelDao.findByNamedQueryAndParams("findMonthStateByMonthAndYear", new String[]{"month", "year"}, new Object[]{monthOneBased, year});
        if (monthStateList.size() == 1) {
            return monthStateList.get(0);
        } else if (monthStateList.size() == 0){
            return null;
        }
        throw new RuntimeException("More than one MonthState found for month " + monthOneBased + "/" + year);
    }
}